package com.example.cadmin.log;

import cn.hutool.core.date.DateUtil;
import com.example.cadmin.Application;
import com.example.cadmin.security.dto.JwtUserDto;
import com.example.cadmin.service.CabinetLogService;
import com.example.cadmin.utils.DateFormat;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 柜体端日志使用
 */
public class CabinetLog {
    public static void insert(int moduleType,String operation, String number) {
        com.example.cadmin.entity.CabinetLog cabinetLog = new com.example.cadmin.entity.CabinetLog();
        //获取用户信息
        JwtUserDto jwtUserDto = (JwtUserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(jwtUserDto != null) {
            String userName = jwtUserDto.getMyUser().getUserName();
            cabinetLog.setUserName(userName);
        }
        //操作的模块1登录模块2借阅记录3操作记录4其他记录
        cabinetLog.setModuleType(moduleType);
        //操作内容
        cabinetLog.setOperation(operation);
        //创建时间
        cabinetLog.setCreateTime(DateFormat.getTime());
        //柜子编号
        cabinetLog.setNumber(number);
        // 静态方法注入bean解决方案
        CabinetLogService cabinetLogService = Application.ac.getBean(CabinetLogService.class);
        cabinetLogService.save(cabinetLog);

    }
}
